using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GhostLogger
{
    public partial class frmDebugInfo : Form
    {

        #region Constructor
        public frmDebugInfo()
        {
            InitializeComponent();
        }
        #endregion


        #region Button Clicks

        private void btnPopulate_Click(object sender, EventArgs e)
        {
            dgvPackages.DataSource = null;
            dgvPackages.Rows.Clear();
            dgvPackages.Columns.Clear();
            prgbarProgress.Visible = true;
            prgbarProgress.Style = ProgressBarStyle.Marquee;
            worker1.RunWorkerAsync();
            //btnPopulate.Enabled = false;

        }

        private void btnAddDebugInfo_Click(object sender, EventArgs e)
        {
            int noOfPacks = DebugInfo.tempDebugPackages.Count;

            if (noOfPacks == 0)
            {
                MessageBox.Show("No package has been selected to add debug information.", "No Packages Selected");
            }
            else
            {
                if (MessageBox.Show("Debug information will be added to the selected " + noOfPacks + " package(s).", "Add Debug Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    //DebugInfo.CompilePackages();
                    prgbarAdd.Visible = true;
                    prgbarAdd.Style = ProgressBarStyle.Marquee;
                    tabMain.Enabled = false;
                    worker2.RunWorkerAsync();
                }
            }
        }

        private void btnDbgInfoPopulate_Click(object sender, EventArgs e)
        {
            txtParamPackage.Text = "";
            txtParamMethod.Text = "";

            dgvParameterList.Rows.Clear();

            prgbarFunctionList.Visible = true;
            prgbarFunctionList.Style = ProgressBarStyle.Marquee;
            worker3.RunWorkerAsync();
        }


        private void btnRemove_Click(object sender, EventArgs e)
        {
            //DebugInfo.RecompilePackages();
            prgbarRemove.Visible = true;
            prgbarRemove.Style = ProgressBarStyle.Marquee;
            worker4.RunWorkerAsync();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            dgvSelectedPackages.Rows.Clear();
            DebugInfo.tempDebugPackages.Clear();
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            DebugInfo.removeDebugPackages.Clear();
            for (int i = 0; i < dgvRemove.Rows.Count; i++)
            {
                DataGridViewCell cellPackage = (DataGridViewCell)dgvRemove.Rows[i].Cells[1];
                DataGridViewCheckBoxCell cellChkBox = (DataGridViewCheckBoxCell)dgvRemove.Rows[i].Cells[0];

                DebugInfo.removeDebugPackages.Add(cellPackage.Value);
                cellChkBox.Value = true;
            }

        }

        private void btnDeleteAll_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to delete all debug information?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DBConnection.DeleteAllData();
                btnDbgInfoPopulate.PerformClick();
            }
        }

        #endregion 

        
        #region dgvPackages Grid Operations

        private void dgvPackages_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int intRow = e.RowIndex;

            if (e.ColumnIndex == 0 && e.RowIndex >= 0)
            {
                DataGridViewCell cellPackage = (DataGridViewCell)dgvPackages.Rows[e.RowIndex].Cells[1];

                if (DebugInfo.debugPackages.Contains(cellPackage.Value) == false &&
                        DebugInfo.tempDebugPackages.Contains(cellPackage.Value) == false)
                {
                    DebugInfo.tempDebugPackages.Add(cellPackage.Value);

                    if (dgvSelectedPackages.Columns.Count == 0)
                    {
                        DataGridViewTextBoxColumn clmPackages = new DataGridViewTextBoxColumn();
                        dgvSelectedPackages.Columns.Insert(0, clmPackages);
                    }

                    Object[] arr = new Object[1];
                    arr[0] = cellPackage.Value;
                    dgvSelectedPackages.Rows.Add(arr);
                    dgvSelectedPackages.AutoResizeColumns();

                    dgvSelectedPackages.Columns[0].DataPropertyName = "package_name";
                    dgvSelectedPackages.Columns[0].HeaderText = "Package Name";

                }
                else
                {
                    MessageBox.Show("Debug information has been already added to this package.", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }



            }


        }



        #endregion


        #region dgvFunctionList Grid Operations

        private void dgvFunctionList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow selectedRow = dgvFunctionList.Rows[e.RowIndex];

                ArrayList userArgs = DebugInfo.GetParamDetails(selectedRow);


                if (dgvParameterList.Columns.Count == 0)
                {

                    DataGridViewTextBoxColumn clmUserArgs = new DataGridViewTextBoxColumn();
                    DataGridViewTextBoxColumn clmValues = new DataGridViewTextBoxColumn();

                    dgvParameterList.Columns.Insert(0, clmUserArgs);
                    dgvParameterList.Columns.Insert(1, clmValues);

                    dgvParameterList.Columns[0].HeaderText = "Name";
                    dgvParameterList.Columns[1].HeaderText = "Value";
                }

                dgvParameterList.Rows.Clear();

                txtParamMethod.Text = selectedRow.Cells["METHOD_NAME"].Value.ToString();
                txtParamPackage.Text = selectedRow.Cells["PACKAGE_NAME"].Value.ToString();

                if (userArgs.Count > 0)
                {
                    IEnumerator enumUserArgs = userArgs.GetEnumerator();
                    int counter = 0;


                    while ((enumUserArgs.MoveNext()) && (counter <= 30))
                    {
                        string strTemp = "S" + (counter + 1);

                        Object[] arr = new Object[2];
                        arr[0] = enumUserArgs.Current;
                        arr[1] = selectedRow.Cells[strTemp].Value.ToString();
                        dgvParameterList.Rows.Add(arr);
                        counter++;
                        //DataGridViewCell userArgsCell = (DataGridViewCell)dgvParameterList.Rows[counter].Cells[0];
                        //userArgsCell.Value = enumUserArgs.Current;

                        //DataGridViewCell valueCell = (DataGridViewCell)dgvParameterList.Rows[counter].Cells[1];
                        //valueCell.Value = selectedRow.Cells[strTemp].Value.ToString();

                    }
                }
                else
                {
                    int counter = 0;

                    while (counter < 30)
                    {
                        string strTemp = "S" + (counter + 1);

                        Object[] arr = new Object[2];
                        arr[0] = strTemp;
                        arr[1] = selectedRow.Cells[strTemp].Value.ToString();
                        dgvParameterList.Rows.Add(arr);
                        counter++;

                    }


                }
                dgvParameterList.AutoResizeColumns();
            }
        }

        #endregion


        #region dgvRemove Grid Operations


        private void dgvRemove_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int intRow = e.RowIndex;

            DataGridViewCell cellPackage = (DataGridViewCell)dgvRemove.Rows[e.RowIndex].Cells["PACKAGE_NAME"];
            DataGridViewCheckBoxCell cellChkBox = (DataGridViewCheckBoxCell)dgvRemove.Rows[e.RowIndex].Cells[0];
            //if (cellPackage.ColumnIndex == 1)
            //{
            //    cellChkBox = (DataGridViewCheckBoxCell)dgvRemove.Rows[e.RowIndex].Cells[0];
            //}
            //else
            //{
            //    cellChkBox = (DataGridViewCheckBoxCell)dgvRemove.Rows[e.RowIndex].Cells[1];
            //}


            bool bolCurrentVal = false;
            if (cellChkBox.Value != null)
            {
                bolCurrentVal = (bool)cellChkBox.Value;
            }

            if (bolCurrentVal == true)
            {
                cellChkBox.Value = false;
                DebugInfo.removeDebugPackages.Remove(cellPackage.Value);
            }
            else
            {
                cellChkBox.Value = true;
                DebugInfo.removeDebugPackages.Add(cellPackage.Value);
            }
        }


        private void dgvRemove_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            MessageBox.Show("Hi");
        }


        #endregion


        #region Workers
        private void worker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DebugInfo.allPackages = DBConnection.RetrievePackages();
        }


        private void worker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            prgbarProgress.Style = ProgressBarStyle.Blocks;
            prgbarProgress.Visible = false;
            dgvPackages.DataSource = DebugInfo.allPackages.Tables["dictionary_sys_package"];

            dgvPackages.Columns[0].DataPropertyName = "package_name";
            dgvPackages.Columns[0].HeaderText = "Package Name";
            dgvPackages.Columns[1].DataPropertyName = "lu_name";
            dgvPackages.Columns[1].HeaderText = "Logical Unit";
            dgvPackages.Columns[2].DataPropertyName = "module";
            dgvPackages.Columns[2].HeaderText = "Module";


            dgvPackages.ReadOnly = true;
            dgvPackages.AutoResizeColumns();

            //DataGridViewCheckBoxColumn chkSelected = new DataGridViewCheckBoxColumn();

            DataGridViewButtonColumn btnSelect = new DataGridViewButtonColumn();
            //dgvPackages.AutoResizeColumns();
            //dgvPackages.AutoResizeRows();

            btnSelect.Text = "Add";
            btnSelect.UseColumnTextForButtonValue = true;

            dgvPackages.Columns.Insert(0, btnSelect);

            //chkSelected.FlatStyle = FlatStyle.Standard;
            //dgvPackages.Columns.Insert(0, chkSelected);

            dgvPackages.Columns[0].DataPropertyName = "select";
            dgvPackages.Columns[0].HeaderText = "Select";

            //Enable searching
            enableSearching();

            //Show the number of Invalid Packages.
            int invalidObjects;
            invalidObjects = DBConnection.GetInvalidPackageCount();
            if (invalidObjects > 0)
            {
                lblInvalidObjects.Show();
                lblInvalidObjects.Text = invalidObjects.ToString() + " invalid packages are not displayed.";    
            }            
        }


        private void worker2_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                DebugInfo.CompilePackages();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void worker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            tabMain.Enabled = true;
            prgbarAdd.Visible = false;
            prgbarAdd.Style = ProgressBarStyle.Blocks;
            MessageBox.Show("Debug Information has been added successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            DebugInfo.tempDebugPackages.Clear();
            dgvSelectedPackages.Rows.Clear();
        }

        private void worker3_DoWork(object sender, DoWorkEventArgs e)
        {
            DebugInfo.dsFunctionList = DBConnection.RetrieveDebugInfo();
        }

        private void worker3_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            dgvFunctionList.DataSource = DebugInfo.dsFunctionList.Tables["c_debug_call_stack_tab"];

            dgvFunctionList.Columns[0].DataPropertyName = "test_case_id";
            dgvFunctionList.Columns[0].HeaderText = "Test Case Id";
            dgvFunctionList.Columns[1].DataPropertyName = "exec_id";
            dgvFunctionList.Columns[1].HeaderText = "Execution Id";
            dgvFunctionList.Columns[2].DataPropertyName = "module";
            dgvFunctionList.Columns[2].HeaderText = "Module";
            dgvFunctionList.Columns[3].DataPropertyName = "package_name";
            dgvFunctionList.Columns[3].HeaderText = "Package Name";
            dgvFunctionList.Columns[4].DataPropertyName = "method_name";
            dgvFunctionList.Columns[4].HeaderText = "Method Name";
            dgvFunctionList.Columns[5].DataPropertyName = "user_id";
            dgvFunctionList.Columns[5].HeaderText = "User Id";
            dgvFunctionList.Columns[5].DataPropertyName = "os_user_name";
            dgvFunctionList.Columns[5].HeaderText = "OS User Name";


            dgvFunctionList.ReadOnly = true;
            dgvFunctionList.AutoResizeColumns();

            prgbarFunctionList.Visible = false;
            prgbarFunctionList.Style = ProgressBarStyle.Blocks;
        }



        private void worker4_DoWork(object sender, DoWorkEventArgs e)
        {
            DebugInfo.RecompilePackages();
        }

        private void worker4_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            prgbarRemove.Visible = false;
            prgbarRemove.Style = ProgressBarStyle.Blocks;
            //tpgAdd.Focus();

            dgvRemove.DataSource = DBConnection.RetrieveDebugAddedHistory().Tables["c_debug_history_tab"];
            dgvRemove.ReadOnly = true;


            if (dgvRemove.Columns.Count < 2)
            {
                DataGridViewCheckBoxColumn chkSelected = new DataGridViewCheckBoxColumn();

                chkSelected.FlatStyle = FlatStyle.Standard;
                dgvRemove.Columns.Insert(1, chkSelected);
            }
        }


        #endregion


        #region Searching

        private void enableSearching()
        {
            txtSearchPackage.ReadOnly = false;
            txtSearchLu.ReadOnly = false;
            txtSearchModule.ReadOnly = false;

            txtSearchPackage.Font = new Font(txtSearchPackage.Font, FontStyle.Italic);
            txtSearchPackage.ForeColor = Color.Gray;
            txtSearchPackage.Text = "PACKAGE";

            txtSearchLu.Font = new Font(txtSearchLu.Font, FontStyle.Italic);
            txtSearchLu.ForeColor = Color.Gray;
            txtSearchLu.Text = "LU";

            txtSearchModule.Font = new Font(txtSearchModule.Font, FontStyle.Italic);
            txtSearchModule.ForeColor = Color.Gray;
            txtSearchModule.Text = "MODULE";
        }

        private void resetSearching()
        {
            txtSearchPackage.Font = new Font(txtSearchPackage.Font, FontStyle.Regular);
            txtSearchPackage.ForeColor = Color.Black;
            txtSearchPackage.Text = "";

            txtSearchLu.Font = new Font(txtSearchLu.Font, FontStyle.Regular);
            txtSearchLu.ForeColor = Color.Black;
            txtSearchLu.Text = "";

            txtSearchModule.Font = new Font(txtSearchModule.Font, FontStyle.Regular);
            txtSearchModule.ForeColor = Color.Black;
            txtSearchModule.Text = "";
        }

        private void doSearch()
        {
            DataView dv = ((DataTable)dgvPackages.DataSource).DefaultView;
            dv.RowFilter = string.Format("lu_name LIKE '{0}*' and module LIKE '{1}*' and package_name LIKE '{2}*'",
                                                            txtSearchLu.Text, txtSearchModule.Text, txtSearchPackage.Text);

        }

        private void txtSearchPackage_TextChanged(object sender, EventArgs e)
        {
            if (txtSearchPackage.ForeColor != Color.Gray)
            {
                doSearch();
            }
        }

        private void txtSearchPackage_Enter(object sender, EventArgs e)
        {
            if (txtSearchPackage.ForeColor == Color.Gray)
            {
                resetSearching();
                //txtSearchPackage.Font = new Font(txtSearchPackage.Font, FontStyle.Regular);
                //txtSearchPackage.ForeColor = Color.Black;
                //if (!String.IsNullOrEmpty(txtSearchPackage.Text))
                //{
                //    txtSearchPackage.SelectionStart = 0;
                //    txtSearchPackage.SelectionLength = txtSearchPackage.Text.Length;
                //}
            }
        }



        private void txtSearchLu_Enter(object sender, EventArgs e)
        {
            if (txtSearchLu.ForeColor == Color.Gray)
            {
                resetSearching();
            }

        }

        private void txtSearchLu_TextChanged(object sender, EventArgs e)
        {
            if (txtSearchLu.ForeColor != Color.Gray)
            {
                doSearch();
            }
        }



        private void txtSearchModule_Enter(object sender, EventArgs e)
        {
            if (txtSearchModule.ForeColor == Color.Gray)
            {
                resetSearching();
            }
        }


        private void txtSearchModule_TextChanged(object sender, EventArgs e)
        {
            if (txtSearchModule.ForeColor != Color.Gray)
            {
                doSearch();
            }
        }


        #endregion


        #region Form Operations

        private void DebugInfo_FormClosed(object sender, FormClosedEventArgs e)
        {
            DBConnection.Close();
            Application.Exit();
        }


        private void frmDebugInfo_SizeChanged(object sender, EventArgs e)
        {
            //if (frmDebugInfo.ActiveForm.WindowState == FormWindowState.Maximized)
            //{
            //    dgvPackages.Width = 


            //}
            ////else
            ////{
            ////    grpAllPackages.Left = (frmDebugInfo.ActiveForm.Width - 921) / 2;
            ////    //grpSelecetedPacks.Left = (frmDebugInfo.ActiveForm.Width - 921) / 2;
            ////}

        }

        #endregion


        #region Tab Operations
        private void tabMain_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPage.Name == "tbgRemove")
            {
                dgvRemove.Columns.Clear();

                dgvRemove.DataSource = DBConnection.RetrieveDebugAddedHistory().Tables["c_debug_history_tab"];
                dgvRemove.ReadOnly = true;

                dgvRemove.Columns[0].Width = 10000;
                //dgvRemove.AutoResizeColumns();


                if (dgvRemove.Columns.Count < 2)
                {
                    DataGridViewCheckBoxColumn chkSelected = new DataGridViewCheckBoxColumn();

                    chkSelected.FlatStyle = FlatStyle.Standard;
                    dgvRemove.Columns.Insert(0, chkSelected);
                }
                dgvRemove.Columns[1].DataPropertyName = "package_name";
                dgvRemove.Columns[1].HeaderText = "Package Name";
                dgvRemove.Columns[0].DataPropertyName = "select";
                dgvRemove.Columns[0].HeaderText = "Select";

                DebugInfo.removeDebugPackages.Clear();
            }
        }
        #endregion

        


       




       

        


       

        

       

       



        

       



        













    }
}